﻿using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;
using System.Windows.Forms;
using System.Drawing;
using ZoomBar2007.Properties;

namespace ZoomBar2007
{
    internal class ZoomBarCommon
    {
        private Component m_component;
        private Rectangle m_bounds;
        private double m_position;
        private bool m_highlightPlus;
        private bool m_highlightMinus;
        private bool m_highlightPlusPressed;
        private bool m_highlightMinusPressed;
        private bool m_highlightTrack;

        private bool m_draggingTrack;

        private const double LeftTrackPos = 25;
        private const double RightTrackPos = 113;
        private const double TrackStep = 4.0;

        public ZoomBarCommon(Component component)
        {
            m_component = component;
            m_draggingTrack = false;
            m_position = 0.0;
        }

        void Invalidate()
        {
            if (m_component is Control)
            {
                (m_component as Control).Invalidate ();
            }
            else if (m_component is ToolStripItem)
            {
                (m_component as ToolStripItem).Invalidate();
            }
        }

        public event EventHandler PositionChanged;

        private void OnPositionChanged()
        {
            if (PositionChanged != null)
                PositionChanged(this, EventArgs.Empty);
        }

        public bool HighlightTrack
        {
            get { return m_highlightTrack; }
            set
            {
                if (m_highlightTrack != value)
                {
                    m_highlightTrack = value;
                    this.Invalidate();
                }
            }
        }

        public bool HighlightPlus
        {
            get { return m_highlightPlus; }
            set
            {
                if (m_highlightPlus != value)
                {
                    m_highlightPlus = value;
                    this.Invalidate();
                }
            }
        }

        public bool HighlightMinus
        {
            get { return m_highlightMinus; }
            set
            {
                if (m_highlightMinus != value)
                {
                    m_highlightMinus = value;
                    this.Invalidate();
                }
            }
        }


        public bool HighlightMinusPressed
        {
            get { return m_highlightMinusPressed; }
            set
            {
                if (m_highlightMinusPressed != value)
                {
                    m_highlightMinusPressed = value;
                    this.Invalidate();
                }
            }
        }

        public bool HighlightPlusPressed
        {
            get { return m_highlightPlusPressed; }
            set
            {
                if (m_highlightPlusPressed != value)
                {
                    m_highlightPlusPressed = value;
                    this.Invalidate();
                }
            }
        }

        public double Position
        {
            get { return m_position; }
            set
            {
                if (value != m_position)
                {
                    if (value < -100.0)
                        m_position = -100.0;
                    else if (value > 100.0)
                        m_position = 100.0;
                    else
                        m_position = value;
                    this.Invalidate();
                    OnPositionChanged();
                }
            }
        }

        public Rectangle Bounds
        {
            get { return m_bounds; }
            set { m_bounds = value; Invalidate(); }
        }

        private Font GetFont()
        {
            return SystemFonts.CaptionFont;
        }


        public void OnPaint(PaintEventArgs e)
        {
            try
            {
                Draw(e.Graphics);
            }
            catch (Exception ex)
            {
                //E.F. In case an exception is thrown during OnPaint - the control stops working and
                // displays a large red X. To prevent it we swallow the exception and log it.
                DrawTextMessage(e.Graphics, this.Bounds, ex.ToString());
            }
        }

        private void DrawTextMessage(Graphics graphics, Rectangle ganttBounds, string text)
        {
            using (StringFormat format = new StringFormat())
            {
                format.Alignment = StringAlignment.Center;
                format.LineAlignment = StringAlignment.Center;
                using (SolidBrush textBrush = new SolidBrush(TextColor))
                {
                    graphics.DrawString(text, GetFont(), textBrush,
                            new RectangleF(ganttBounds.Left, ganttBounds.Top, ganttBounds.Width, ganttBounds.Height), format);
                }
            }
        }

        private enum PositionType { None, Plus, Minus, Track }

        public Color TextColor
        {
            get { return Color.Black; }
        }

        private PositionType GetPointPosition(Point p)
        {
            double pos = GetPositionAtPoint(p.X);
            if (Math.Abs(pos - this.Position) <= 8.0)
                return PositionType.Track;

            double dist = Math.Sqrt(Math.Pow(p.X - 10, 2) + Math.Pow(p.Y - 9, 2));
            if (dist <= 9.0)
                return PositionType.Minus;
            dist = Math.Sqrt(Math.Pow(p.X - 128, 2) + Math.Pow(p.Y - 9, 2));
            if (dist <= 9.0)
                return PositionType.Plus;
            return PositionType.None;

        }
        public void OnMouseDown(MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                PositionType posType = GetPointPosition(e.Location);
                if (posType == PositionType.Track)
                {
                    m_draggingTrack = true;
                }
                else
                {
                    m_draggingTrack = false;
                    double pointPos = GetPositionAtPoint(e.Location.X);
                    if (pointPos >= -100.0 && pointPos <= 100.0)
                    {
                        this.Position = pointPos;
                    }
                }
                if (posType == PositionType.Minus)
                {
                    HighlightMinusPressed = true;
                    HighlightPlusPressed = false;
                    this.Position -= TrackStep;
                }
                else if (posType == PositionType.Plus)
                {
                    HighlightMinusPressed = false;
                    HighlightPlusPressed = true;
                    this.Position += TrackStep;
                }
                else
                {
                    HighlightMinusPressed = false;
                    HighlightPlusPressed = false;
                }
            }
            else
            {
                m_draggingTrack = false;
                HighlightMinusPressed = false;
                HighlightPlusPressed = false;
            }

        }

        public void OnMouseUp(MouseEventArgs e)
        {
            m_draggingTrack = false;
            HighlightMinusPressed = false;
            HighlightPlusPressed = false;
        }

        public void OnMouseMove(MouseEventArgs e)
        {
            PositionType pos = GetPointPosition(e.Location);
            if (pos == PositionType.Minus)
            {
                HighlightMinus = true;
                HighlightPlus = false;
            }
            else if (pos == PositionType.Plus)
            {
                HighlightMinus = false;
                HighlightPlus = true;
            }
            else
            {
                HighlightMinus = false;
                HighlightPlus = false;
            }

            if (pos == PositionType.Track)
            {
                HighlightTrack = true;
            }
            else
            {
                HighlightTrack = false;
            }

            if (m_draggingTrack)
            {
                if (e.Button == MouseButtons.Left)
                {
                    this.Position = GetPositionAtPoint(e.Location.X);
                }
                else
                {
                    m_draggingTrack = false;
                }
            }
        }

        private double GetPositionAtPoint(int x)
        {
            double pos = (x - LeftTrackPos) / (RightTrackPos - LeftTrackPos) * 200.0 - 100.0;
            return pos;
        }

        private void Draw(Graphics graphics)
        {
            int x = this.Bounds.X;
            int y = this.Bounds.Y;
            int width = this.Bounds.Width;
            int height = this.Bounds.Height;
            if (HighlightPlus)
            {
                if (HighlightPlusPressed)
                {
                    graphics.DrawImage(Resources.trackBarPlusPressed, new Point(0, 0));
                }
                else
                {
                    graphics.DrawImage(Resources.trackBarPlus, new Point(0, 0));
                }
            }
            else if (HighlightMinus)
            {
                if (HighlightMinusPressed)
                {
                    graphics.DrawImage(Resources.trackBarMinusPressed, new Point(0, 0));
                }
                else
                {
                    graphics.DrawImage(Resources.trackBarMinus, new Point(0, 0));
                }
            }
            else
            {
                graphics.DrawImage(Resources.trackBarEmpty, new Point(0, 0));
            }

            double trackPosition = LeftTrackPos + ((double)this.Position + 100.0) / 200.0 * (RightTrackPos - LeftTrackPos);

            Point trackPoint = new Point((int)Math.Round(trackPosition - (double)Resources.trackBarCusorBlue.Width / 2.0), 0);
            if (HighlightTrack)
            {
                graphics.DrawImage(Resources.trackBarCusorYellow, trackPoint);
            }
            else
            {
                graphics.DrawImage(Resources.trackBarCusorBlue, trackPoint);
            }
        }

        public void OnResize(EventArgs e)
        {
            this.Invalidate();
        }

    }
}
